<?php

namespace App\Controller;

use Sensio\Bundle\FrameworkExtraBundle\Configuration\Cache;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;

class Test1Controller extends Controller
{
    /**
     * @Route("/test", name="test")
     * @Cache(smaxage="10")
     */
    public function index(Request $request)
    {
        $productId = '1231234';
        /**
         * @var \Doctrine\Common\Cache\Cache
         */
        $cacheProduct = $this->get('cache.app')->getItem($productId);
        if (!$cacheProduct->isHit()) {
            $sex_data = [
                '女' => 1,
                '男' => 2,
            ];
            $cacheProduct->set($sex_data);
            $this->get('cache.app')->save($cacheProduct);
        } else {
            $sex_data = $cacheProduct->get();
        }
        //print_r($sex_data);exit;
        $form = $this->createFormBuilder()
            ->add('name')
            ->add('sex', ChoiceType::class, [
                'choices' => $sex_data,
                /*                'choices' => [
                                    '女' => 1,
                                    '男' => 2,
                                ],*/
                'attr' => ['rows' => 20],
                'label' => 'label.content',
            ])
            ->add('save', SubmitType::class)
            ->getForm();
        $form->handleRequest($request);
        if ($form->isSubmitted() && $form->isValid()) {
            var_dump($form->getData());
        }
        return $this->render('test/index.html.twig', [
            'controller_name' => 'TestController',
            'form' => $form->createView(),
        ]);
    }
}
